<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//文章帖子表
class Bbs_article_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->bbs_article = 'bbs_article';
        $this->bbs_article_category = 'bbs_article_category';
        $this->users_info = 'users_info';
        $this->bbs_count = 'bbs_count';
        $this->bbs_focus = 'bbs_focus';
        $this->bbs_article_comment = 'bbs_article_comment';
        $this->bbs_favorite = 'bbs_favorite';
        $this->bbs_notice = 'bbs_notice';
    }
    //获取文章单个字段的值
    function get_article_simple_column($where,$column){
        $this->db->select($column);
        $this->db->from($this->bbs_article);
        $this->db->where($where);
        $result = $this->db->get()->row_array();
        return $result[$column];
        
    }
    
    //获取一级分类下面热门文章总数
    function get_one_hots_count($pid){
        $sql = "SELECT COUNT(*) count 
                FROM tan_bbs_article 
                WHERE hot = 1 AND tan_bbs_article.show = 1 AND tan_bbs_article.status = 1 AND cat_id IN (SELECT id FROM tan_bbs_article_category WHERE pid=$pid)";
        $count = $this->db->query($sql)->row_array();
        return $count['count'];
    }
    //获取一级分类下面热门文章列表
    function get_one_hots_list($pid,$page,$limit){
        $sql = "SELECT tan_bbs_article.*,tan_bbs_article_category.name cat_name,tan_bbs_article_category.code cat_code,tan_users_info.nickname
                FROM tan_bbs_article  
                LEFT JOIN tan_bbs_article_category ON tan_bbs_article.cat_id = tan_bbs_article_category.id
                LEFT JOIN tan_users_info ON tan_bbs_article.authorid = tan_users_info.user_id
                WHERE hot = 1 AND tan_bbs_article.show = 1 AND tan_bbs_article.status = 1 AND cat_id IN (SELECT id FROM tan_bbs_article_category WHERE pid=$pid) 
                ORDER BY sort DESC
                LIMIT $page,$limit";
        $list = $this->db->query($sql)->result_array();
        return $list;
    }
    
    //获取一级分类下面最新文章总数
    function get_one_fresh_count($pid){
        $sql = "SELECT COUNT(*) count 
                FROM tan_bbs_article 
                WHERE hot = 1 AND tan_bbs_article.show = 1 AND tan_bbs_article.status = 1 AND cat_id IN (SELECT id FROM tan_bbs_article_category WHERE pid=$pid)";
        $count = $this->db->query($sql)->row_array();
        return $count['count'];
    }
    //获取一级分类下面最新文章列表
    function get_one_fresh_list($pid,$page,$limit){
        $sql = "SELECT tan_bbs_article.*,tan_bbs_article_category.name cat_name,tan_bbs_article_category.code cat_code,tan_users_info.nickname
                FROM tan_bbs_article  
                LEFT JOIN tan_bbs_article_category ON tan_bbs_article.cat_id = tan_bbs_article_category.id
                LEFT JOIN tan_users_info ON tan_bbs_article.authorid = tan_users_info.user_id
                WHERE  tan_bbs_article.show = 1 AND tan_bbs_article.status = 1 AND cat_id IN (SELECT id FROM tan_bbs_article_category WHERE pid=$pid) 
                ORDER BY sort DESC,create_time DESC
                LIMIT $page,$limit";
        $list = $this->db->query($sql)->result_array();
        return $list;
    }
    
    //获取帖子所属分类
    function get_article_category($where){
        $this->db->select('*');
        $this->db->from($this->bbs_article_category);
        $this->db->where($where);
        return $this->db->get()->row_array();
    }
    
    //普通帖子列表
    function get_article_list($where,$page,$limit){
        $this->db->select($this->bbs_article.'.*,'.$this->bbs_article_category.'.name cat_name,'.$this->bbs_article_category.'.code cat_code,'.$this->users_info.'.nickname');
        $this->db->from($this->bbs_article);
        $this->db->join($this->bbs_article_category,$this->bbs_article_category.'.id='.$this->bbs_article.'.cat_id',left);
        $this->db->join($this->users_info,$this->users_info.'.user_id='.$this->bbs_article.'.authorid',left);
        $this->db->where($where);
        $this->db->order_by($this->bbs_article.".stick DESC,".$this->bbs_article.'.sort DESC,'.$this->bbs_article.".create_time DESC");
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    
    //热门帖子列表
    function get_article_hot_list($where,$page,$limit){
        $this->db->select($this->bbs_article.'.*,'.$this->bbs_article_category.'.name cat_name,'.$this->bbs_article_category.'.code cat_code,'.$this->users_info.'.nickname');
        $this->db->from($this->bbs_article);
        $this->db->join($this->bbs_article_category,$this->bbs_article_category.'.id='.$this->bbs_article.'.cat_id',left);
        $this->db->join($this->users_info,$this->users_info.'.user_id='.$this->bbs_article.'.authorid',left);
        $this->db->where($where);
        $this->db->order_by($this->bbs_article.".stick DESC,".$this->bbs_article.'.sort DESC,'.$this->bbs_article.".create_time DESC");
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    
    //社区个人用户热门帖子列表
    function get_article_user_list($where,$page,$limit){
        $this->db->select($this->bbs_article.'.id,'.
                $this->bbs_article.'.title,'.
                $this->bbs_article.'.summary,'.
                $this->bbs_article.'.commentnum,'.
                $this->bbs_article.'.thumb,'.
                $this->bbs_article.'.likes,'.
                $this->bbs_article.'.status,'.
                $this->bbs_article.'.show,'.
                $this->bbs_article.'.create_time,'.
                $this->bbs_article_category.'.name cat_name,'.
                $this->bbs_article_category.'.group cat_group,'.
                $this->bbs_article_category.'.code cat_code');
        $this->db->from($this->bbs_article);
        $this->db->join($this->bbs_article_category,$this->bbs_article_category.'.id='.$this->bbs_article.'.cat_id',left);
        $this->db->where($where);
        $this->db->order_by($this->bbs_article.'.sort DESC,'.$this->bbs_article.".create_time DESC");
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    //社区个人用户帖子评论总数
    function get_article_user_comment_count($where){
        $this->db->select($this->bbs_article_comment.'.*');
        $this->db->from($this->bbs_article_comment);
        $this->db->where($where);
        return $this->db->get()->num_rows();
    }
    //社区个人用户帖子评论列表
    function get_article_user_comment_list($where,$page,$limit){
        $this->db->select($this->bbs_article_comment.'.*,'.
                $this->bbs_article.'.title,u1.nickname reply_user_nickname,u2.nickname comment_user_nickname,u2.head_img comment_user_head_img,'.$this->bbs_article.'.title,'.$this->bbs_article.'.cat_id');
        $this->db->from($this->bbs_article_comment);
        $this->db->join($this->bbs_article,$this->bbs_article.'.id='.$this->bbs_article_comment.'.article_id',left);
        $this->db->join($this->users_info." u1",$this->bbs_article_comment.'.repay_user_id='.'u1.user_id',left);
        $this->db->join($this->users_info." u2",$this->bbs_article_comment.'.uid='.'u2.user_id',left);
        $this->db->where($where);
        $this->db->order_by($this->bbs_article_comment.".create_time DESC");
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    
    //获取个人用户文章评论信息
    function get_article_user_comment($where){
        $this->db->select($this->bbs_article_comment.'.*,'.
                $this->bbs_article.'.title,'.
                $this->bbs_article.'.id title_id,'.
                'u2.nickname comment_user_nickname,u2.head_img comment_user_head_img');
        $this->db->from($this->bbs_article_comment);
        $this->db->join($this->bbs_article,$this->bbs_article.'.id='.$this->bbs_article_comment.'.article_id',left);
        $this->db->join($this->users_info." u2",$this->bbs_article_comment.'.uid='.'u2.user_id',left);
        $this->db->where($where);
        $this->db->order_by($this->bbs_article_comment.".create_time DESC");
        return $this->db->get()->row_array();
    }
    
    //获取个人用户评论回复信息
    function get_article_user_reply($where){
        $this->db->select($this->bbs_article_comment.'.*,'.
                $this->bbs_article.'.title,'.
                $this->bbs_article.'.id title_id,'.
                'u2.nickname reply_user_nickname,u2.head_img reply_user_head_img');
        $this->db->from($this->bbs_article_comment);
        $this->db->join($this->bbs_article,$this->bbs_article.'.id='.$this->bbs_article_comment.'.article_id',left);
        $this->db->join($this->users_info." u2",$this->bbs_article_comment.'.uid='.'u2.user_id',left);
        $this->db->where($where);
        $this->db->order_by($this->bbs_article_comment.".create_time DESC");
        return $this->db->get()->row_array();
    }
    //获取社区用户个人收藏文章列表
    function get_article_user_favorite_list($where,$page,$limit){
        $this->db->select($this->bbs_favorite.'.*,'.
                $this->bbs_article.".title,".
                $this->bbs_article.".likes,".
                $this->bbs_article.".cat_id,".
                $this->bbs_article.".summary,".
                $this->bbs_article.".thumb,".
                $this->bbs_article.".authorid,".
                $this->bbs_article.".likes,".
                $this->bbs_article.".commentnum");
        $this->db->from($this->bbs_favorite);
        $this->db->join($this->bbs_article,$this->bbs_article.'.id='.$this->bbs_favorite.'.article_id',"left");
        $this->db->where($where);
        $this->db->order_by($this->bbs_favorite.".create_time DESC");
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    
    
    function get_count($where){
        $this->db->select('*');
        $this->db->from($this->bbs_article);
        $this->db->where($where);
        $this->db->order_by('create_time','desc');
        return $this->db->get()->num_rows();
    }
    function get_list($where,$page,$limit){
        $this->db->select(
                $this->bbs_article.'.id,'.
                $this->bbs_article.'.title,'.
                $this->bbs_article.'.summary,'.
                $this->bbs_article.'.cat_id,'.
                $this->bbs_article.'.thumb,'.
                $this->bbs_article.'.likes,'.
                $this->bbs_article.'.commentnum,'.
                $this->bbs_article.'.status,'.
                $this->bbs_article.'.is_comment,'.
                $this->bbs_article.'.is_award,'.
                $this->bbs_article.'.create_time,'.
                $this->bbs_article_category.'.name cat_name,'.
                $this->bbs_article_category.'.code cat_code,'.
                $this->bbs_article_category.".group cat_group");
        $this->db->from($this->bbs_article);
        $this->db->join($this->bbs_article_category,$this->bbs_article_category.'.id='.$this->bbs_article.'.cat_id',"left");
        $this->db->where($where);
        $this->db->order_by($this->bbs_article.".create_time DESC");
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    
    //获取社区个人的热门文章
    function get_person_hot_articles_list($where,$page,$limit,$where_not_in){
        $this->db->select(
                $this->bbs_article.'.id,'.
                $this->bbs_article.'.title,'.
                $this->bbs_article.'.readnum,'.
                $this->bbs_article.'.commentnum,'.
                $this->bbs_article.'.create_time,'.
                $this->bbs_article_category.'.name cat_name,'.
                $this->bbs_article_category.".code cat_code,".
                $this->bbs_article_category.".group cat_group");
        $this->db->from($this->bbs_article);
        $this->db->join($this->bbs_article_category,$this->bbs_article_category.'.id='.$this->bbs_article.'.cat_id',left);
        $this->db->where($where);
        if($where_not_in){
            $this->db->where_not_in($this->bbs_article.".id",$where_not_in);
        }
        $this->db->order_by($this->bbs_article.'.commentnum','desc');
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    
    //获取明星用户或作家用户的热门文章
    function get_special_person_hot_articles_list($where,$page,$limit,$users_id_in){
        $this->db->select(
                $this->bbs_article.'.id,'.
                $this->bbs_article.'.title,'.
                $this->bbs_article.'.readnum,'.
                $this->bbs_article.'.likes,'.
                $this->bbs_article.'.commentnum,'.
                $this->bbs_article.'.create_time,'.
                $this->bbs_article_category.'.name cat_name,'.
                $this->bbs_article_category.".code cat_code,".
                $this->bbs_article_category.".group cat_group");
        $this->db->from($this->bbs_article);
        $this->db->join($this->bbs_article_category,$this->bbs_article_category.'.id='.$this->bbs_article.'.cat_id',left);
        $this->db->where($where);
        if($users_id_in){
            $this->db->where_in($this->bbs_article.".authorid",$users_id_in);
        }
        $this->db->order_by($this->bbs_article.'.commentnum','desc');
        $this->db->limit($limit,$page);
        return $this->db->get()->result_array();
    }
    function stick($cat_id){
        $this->db->select($this->bbs_article.'.*,'.$this->bbs_article_category.'.name,'.$this->users_info.'.nickname');
        $this->db->from($this->bbs_article);
        $this->db->join($this->bbs_article_category,$this->bbs_article_category.'.id='.$this->bbs_article.'.cat_id',left);
        $this->db->join($this->users_info,$this->users_info.'.user_id='.$this->bbs_article.'.authorid',left);
        $this->db->where('cat_id',$cat_id);
        $this->db->where('stick',1);
        $this->db->order_by($this->bbs_article.'.create_time','desc');
        $this->db->limit(2,0);
        return $this->db->get()->result_array();
    }
    //添加帖子文章
    function insert($data){
        $this->db->insert($this->bbs_article, $data);
        $insert_id = $this->db->insert_id();
        if($this->db->affected_rows()){
           $this->db->set('articles_count','articles_count + 1',FALSE);
           $this->db->where('user_id',$data['authorid']);
           $this->db->update($this->bbs_count);
        }
        return $insert_id;
    }
    //获取cate code
    function get_catecode($cat_id){
        $this->db->select('code');
        $this->db->from($this->bbs_article_category);
        $this->db->where("id",$cat_id);
        $result=$this->db->get()->row_array();
        return $result['code'];
    }
    //判断是否关注了板块
    function get_category_focus($where){
        return $this->db->where('user_id',$where['user_id'])->where("cat_id",$where["cat_id"])->get($this->bbs_focus)->row_array();
    }
    //增加关注板块记录
    function insert_category_focus($data){
        $this->db->insert($this->bbs_focus, $data);
        return $this->db->affected_rows();
    }
    //取消关注板块记录
    function delete_category_focus($where){
        $this->db->where("user_id",$where['user_id'])->where("cat_id",$where['cat_id'])->delete($this->bbs_focus);
        //$this->db->where("recieve_id",$where['user_id'])->where("operator_id",$where['operator_id'])->delete($this->bbs_notice);
    }
    //更新文章
    function update_article($data,$article_id){
        return $this->db->where('id',$article_id)->update($this->bbs_article, $data);
    } 
    
    
    
    //获取用户帖子总阅读数量
    function get_article_readnum($where){
        $this->db->select('SUM(readnum) readnum');
        $this->db->from($this->bbs_article);
        $this->db->where($where);
        return $this->db->get()->row_array();
    }
    
}
